package 设计模式.单例模式;


/**
 * 双重校验锁实现对象单例（线程安全）
 */

class double_test_singleton {
//采用volatile关键字修饰
//懒汉式
private volatile static double_test_singleton uniqueInstance;
private double_test_singleton()
    {
    }
    public static double_test_singleton getInstance()
    {
        //先判断对象是否已经实例过，没有实例过进入加锁状态
        if(uniqueInstance==null)
        {
            //类对象加锁
            synchronized (double_test_singleton.class){
                if (uniqueInstance==null)
                {
                    uniqueInstance=new double_test_singleton();
                }
            }
        }
        return uniqueInstance;
    }
}
public class test_singleton{
    public static void main(String[] args) {
        double_test_singleton instance1 = double_test_singleton.getInstance();
        double_test_singleton instance2 = double_test_singleton.getInstance();
        System.out.println(instance1==instance2);
    }
}